package Thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Demo22 {
    public static void main(String[] args) throws InterruptedException {
        //任务队列
        ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(100);

        //拒绝策略:忽略最新任务/丢弃新来的任务
        RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();

        //创建线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5,10,60L, TimeUnit.SECONDS,queue,handler);

        //提交任务到线程池
        for (int i = 0; i < 120; i++) {
            int id = i;
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    Thread current = Thread.currentThread();
                    System.out.println("Hello thread " + id + "," + current.getName() + "正在运行");
                }
            });
        }

        Thread.sleep(2000);

        //关闭线程池
        pool.shutdown();
        System.out.println("程序退出");
    }
}
